import {DocTool} from "../../../common/docTool";
import {getEchartsBase64} from "../../../common/utils";
import QuanlityControl from "../analysis/components/quanlityControl/QuanlityControl";

export const TREE_DATA = [
  {key: '0-hasLogo', title: 'Logo'},
  {
    key: '1-hasHeader',
    title: 'Header',
    children: [
      {key: '2-hasTitle', title: 'Title'},
      {key: '3-hasSubTitle', title: 'Sub Title'}
      // {key: '4-hasLogo', title: 'Logo'}
    ]
  },
  {
    key: '5-hasExperiment',
    title: 'Experiment',
    children: [
      {key: '6-hasInformation', title: 'Information'},
      {key: '7-hasExperimentSetting', title: 'Experiment Setting'},
      {key: '8-hasParameterSetting', title: 'Parameter Setting'},
      {key: '9-hasComments', title: 'Comments'}
      // {key: '10-hasLoadPreview', title: 'Load Preview'}
    ]
  },
  {
    key: '11-hasPlate',
    title: 'Plate Edit',
    children: [
      {key: '12-hasPlateLayout', title: 'Plate Edit'},
      {key: '13-hasTask', title: 'Task'},
      {key: '14-hasTarget', title: 'Target'},
      {key: '15-hasSplitPlate', title: 'Split Plate'},
      {key: '16-hasSample', title: 'Sample'},
      {key: '17-hasStandard', title: 'Standard'}
    ]
  },
  {
    key: '18-hasProtocol',
    title: 'Protocol Edit',
    children: [
      {key: '19-hasProtocol', title: 'Plate Edit'}
    ]
  },
  {
    key: '20-hasRun',
    title: 'Run',
    children: [
      {key: '21-hasRunTime', title: 'Run Time'},
      {key: '22-hasTemperatureCurve', title: 'Temperature Curve'}
    ]
  },
  {
    key: '23-hasAnalysis',
    title: 'Analysis',
    children: [
      {key: '24-hasQuantification', title: 'Quantification'},
      {key: '25-hasStandardCurve', title: 'Standard Curve'},
      {key: '26-hasQuantificationData', title: 'Quantification Data'},
      {key: '27-hasMelt', title: 'Melt Curve'},
      {key: '28-hasMeltData', title: 'Melt Data'},
      {key: '29-hasGeneExpression', title: 'Gene Expression'},
      {key: '30-hasAllelic', title: 'Allelic'},
      {key: '31-hasQuanlityControl', title: 'Quanlity Control'},
      {key: '32-hasEndPoint', title: 'End-Point'}
    ]
  }
];

export const TREE_DATA_CN = [
  {key: '0-hasLogo', title: 'Logo'},
  {
    key: '1-hasHeader',
    title: '标题',
    children: [
      {key: '2-hasTitle', title: '标题'},
      {key: '3-hasSubTitle', title: '副标题'}
      // {key: '4-hasLogo', title: 'Logo'}
    ]
  },
  {
    key: '5-hasExperiment',
    title: '实验',
    children: [
      {key: '6-hasInformation', title: '信息'},
      {key: '7-hasExperimentSetting', title: '实验设置'},
      {key: '8-hasParameterSetting', title: '参数设置'},
      {key: '9-hasComments', title: '备注'}
      // {key: '10-hasLoadPreview', title: 'Load Preview'}
    ]
  },
  {
    key: '11-hasPlate',
    title: '孔板设置',
    children: [
      {key: '12-hasPlateLayout', title: '孔板设置'},
      {key: '13-hasTask', title: '类型'},
      {key: '14-hasTarget', title: '目标基因'},
      // {key: '15-hasSplitPlate', title: 'Split Plate'},
      {key: '16-hasSample', title: '实验样品'},
      {key: '17-hasStandard', title: '标准曲线'}
    ]
  },
  {
    key: '18-hasProtocol',
    title: '程序设置',
    children: [
      {key: '19-hasProtocol', title: '孔板设置'}
    ]
  },
  {
    key: '20-hasRun',
    title: '实时运行',
    children: [
      {key: '21-hasRunTime', title: '运行时间'},
      {key: '22-hasTemperatureCurve', title: '温度曲线'}
    ]
  },
  {
    key: '23-hasAnalysis',
    title: '数据分析',
    children: [
      {key: '24-hasQuantification', title: '定量分析'},
      {key: '25-hasStandardCurve', title: '标准曲线'},
      {key: '26-hasQuantificationData', title: '定量结果'},
      {key: '27-hasMelt', title: '熔解曲线'},
      {key: '28-hasMeltData', title: '熔解曲线结果'},
      {key: '29-hasGeneExpression', title: '基因表达分析'},
      {key: '30-hasAllelic', title: '等位基因分析'},
      {key: '31-hasQuanlityControl', title: '质量控制分析'},
      {key: '32-hasEndPoint', title: '终点法分析'}
    ]
  }
];

export function updateReport(data = {}, isUpdateDate = true) {
  let doc = new DocTool("container");
  if (data.hasLogo && data.header.imgLogo) {
    doc.createImgFloatWithBase64(data.header.imgLogo, {type: "png", float: "right", width: "100px", height: "100px"});
    // doc.createImgInLineWithPath(logoImg, {title: "logo", alt: "logo", align: "left"});
  }
  if (data.hasHeader) {
    let header = data.header;
    // if (data.hasLogo) {
    //   if (header.imgLogo) {
    //     doc.createImgFloatWithBase64(header.imgLogo, {type: "png", float: "right", width: "100px", height: "100px"});
    //   } else {
    //     doc.createImgFloatWithBase64(header.imgLogo, {type: "png", float: "right"});
    //   }
    // }
    if (header.hasTitle) {
      doc.createTitle(header.title, {textAlign: header.headerAlign});
    }
    if (header.hasSubTitle) {
      doc.createSubTitle(header.subTitle, {textAlign: header.headerAlign});
    }
  }
  if (data.hasExperiment) {
    doc.createH1("Experiment");
    let experiment = data.experiment;
    if (experiment.hasInformation) {
      doc.createH2("Information");
      let information = experiment.information;
      doc.createText("Name : ", information.name);
      doc.createText("Barcode : ", information.barcode);
      doc.createText("User Name : ", information.userName);
      doc.createText("Instrument Name : ", information.instrumentName);
      doc.createText("Block Type : ", information.blockType);
    }
    if (experiment.hasExperimentSetting) {
      doc.createH2("Experiment Setting");
      let experimentSetting = experiment.experimentSetting;
      doc.createText("Experiment Identity : ", experimentSetting.experimentIdentify);
      doc.createText("Regents Type : ", experimentSetting.reagentsType);
      doc.createText("Run Mode : ", experimentSetting.runMode);
    }
    if (experiment.hasParameterSetting) {
      doc.createH2("Parameter Setting");
      let parameterSetting = experiment.parameterSetting;
      doc.createText("Reaction Volume : ", parameterSetting.reactionVolume + "μL");
      doc.createText("Lid Heating Switch : ", parameterSetting.lidHeatingSwitch);
      doc.createText("Cover Temp : ", parameterSetting.coverTemp + "℃");
      doc.createText("Lid Close Temp : ", parameterSetting.lidCloseTemp + "℃");
    }
    if (experiment.hasComments) {
      doc.createH2("Comments");
      doc.createText(experiment.comments);
    }
    if (experiment.hasLoadPreview) {
      doc.createH2("Load Preview");
      doc.createImgInLineWithBase64(experiment.imgLoadPreviewPlate);
      doc.createImgInLineWithBase64(experiment.imgLoadPreviewProtocol);
    }
  }
  if (data.hasPlate) {
    let plate = data.plate;
    doc.createH1("Plate edit");
    if (plate.hasPlateLayout) {
      doc.createH2("Plate Edit");
      doc.createTable(plate.plateLayout, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}, leftHeadNum: 1, leftHeadStyle: {fontWeight: "bold"}});
    }
    if (plate.hasTask) {
      doc.createH2("Task");
      plate.task.split("\n").forEach(text => doc.createText(text));
    }
    if (plate.hasTarget) {
      doc.createH2("Target");
      doc.createTable(plate.target, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (plate.hasSplitPlate) {
      doc.createH2("Split Plate");
      plate.splitPlate.split("\n").forEach(text => doc.createText(text));
    }
    if (plate.hasSample) {
      doc.createH2("Sample");
      doc.createTable(plate.sample, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (plate.hasStandard) {
      doc.createH2("Standard");
      let standard = plate.standard;
      doc.createText("Select Target : ", standard.selectTarget);
      doc.createText("Standard Amount : ", standard.standardAmount);
      doc.createText("Standard Number : ", standard.standardNumber);
      doc.createText("Factor : ", standard.factor);
      doc.createText("Dilution Factor : ", standard.dilutionFactor);
      doc.createText("Unit : ", standard.unit);
      doc.createText("Replicates : ", standard.replicates);
      doc.createText("Increasing/Decreasing : ", standard.increasing_decreasing);
      //  TODO:
      //    doc.createText("Assignment : ", "");
    }
  }
  if (data.hasProtocol) {
    doc.createH1("Protocol Edit");
    let protocol = data.protocol;
    if (protocol.hasProtocol) {
      doc.createH2("Protocol");
      let arr = [];
      protocol.protocol.split("\n").forEach(text => {
        if (text.includes("\t")) {
          arr[arr.length] = text.split("\t");
        } else {
          if (arr.length !== 0) {
            doc.createTable(arr);
            arr = [];
          }
          doc.createText(text);
        }
      });
      if (arr.length !== 0) {
        doc.createTable(arr);
      }
    }
    doc.createText("Protocol graph", "");
    doc.createImgInLineWithBase64(protocol.imgProtocolGraph, {style: {width: "100%", backgroundColor: "#213040"}});
    let arr = [];
    protocol.advancedSetting.split("\n").forEach(text => {
      if (text.includes("\t")) {
        arr[arr.length] = text.split("\t");
      } else {
        if (arr.length !== 0) {
          doc.createTable(arr);
          arr = [];
        }
        doc.createText(text);
      }
    });
    if (arr.length !== 0) {
      doc.createTable(arr);
    }
  }
  if (data.hasRun) {
    doc.createH1("Run");
    let run = data.run;
    if (run.hasRunTime) {
      doc.createH2("Run time");
      run.runTime.split("\n").forEach(text => doc.createText(text));
    }
    if (run.hasTemperatureCurve) {
      doc.createH2("Temperature curve");
      doc.createImgInLineWithBase64(run.imgTemperatureCurve, {style: {width: "100%"}});
    }
  }
  if (data.hasAnalysis) {
    doc.createH1("Analysis");
    let analysis = data.analysis;
    if (analysis.hasQuantification) {
      doc.createH2("Quantification");
      doc.createText("Amplification curve-Cq", "");
      doc.createImgInLineWithBase64(analysis.imgAmplificationCurve_cq, {style: {transform: "scale(0.45)", position: "relative", left: "-193px"}});
      doc.createText("Amplification curve-log", "");
      doc.createImgInLineWithBase64(analysis.imgAmplificationCurve_log, {style: {transform: "scale(0.9)", position: "relative", left: "-60px"}});
    }
    if (analysis.hasStandardCurve) {
      doc.createH2("Standard Curve");
      doc.createImgInLineWithBase64(analysis.imgStandardCurve, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      analysis.standardCurveFormulas.forEach(formula => doc.createText(formula));
    }
    if (analysis.hasQuantificationData) {
      doc.createH2("Quantification data");
      doc.createTable(analysis.quantificationData, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (analysis.hasMelt) {
      doc.createH2("Melt curve");
      doc.createText("Melt curve");
      doc.createImgInLineWithBase64(analysis.imgMeltCurve, {style: {transform: "scale(0.6)", position: "relative", left: "-150px"}});
      doc.createText("Melt Peak");
      doc.createImgInLineWithBase64(analysis.imgMeltPeak, {style: {transform: "scale(0.7)", position: "relative", left: "-100px"}});
    }
    if (analysis.hasMeltData) {
      doc.createH2("Melt data");
      doc.createTable(analysis.meltData, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (analysis.hasGeneExpression) {
      doc.createH2("Gene expression");
      doc.createText("Bar Chart", "");//, backgroundColor: 'rgba(194,196,217,0.35)'
      doc.createImgInLineWithBase64(analysis.imgGeneExpressionBar, {style: {transform: "scale(0.2)", width: '485%', position: "relative", left: "-193%"}});
      // doc.createText("Mode : ", analysis.geneExpressionMode);
      doc.createText("Mode : ", "");
      analysis.geneExpressionMode.split("\n").forEach((item, index) => {
        doc.createText(item);
      });
      doc.createText("gene expression data", "");
      doc.createTable(analysis.geneExpressionData);
    }
    if (analysis.hasAllelic) {
      doc.createH1("Allelic");
      doc.createText("Allelic_cq", "");
      doc.createImgInLineWithBase64(analysis.imgAllelic_cq, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      doc.createText("Allelic_rfu", "");
      doc.createImgInLineWithBase64(analysis.imgAllelic_rfu, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      doc.createH1("AssigningCalls Data");
      doc.createTable(analysis.allelicData);
    }
    if (analysis.hasQuanlityControl) {
      doc.createH2("QuanlityControlData");
      doc.createTable(analysis.failureReportData);
    }
    if (analysis.hasEndPoint) {
      doc.createH2("End-Point");
      doc.createText("EndPoint Setting Data", "");
      doc.createTable(analysis.endPointSettingData);
      doc.createText("RFU Result", "");
      doc.createTable(analysis.endPointData);
    }
  }
  console.log(data);
}

export function updateReportCn(data = {}, isUpdateDate = true) {
  let doc = new DocTool("container");
  if (data.hasLogo && data.header.imgLogo) {
    doc.createImgFloatWithBase64(data.header.imgLogo, {type: "png", float: "right", width: "100px", height: "100px"});
    // doc.createImgInLineWithPath(logoImg, {title: "logo", alt: "logo", align: "left"});
  }
  if (data.hasHeader) {
    let header = data.header;
    // if (data.hasLogo) {
    //   if (header.imgLogo) {
    //     doc.createImgFloatWithBase64(header.imgLogo, {type: "png", float: "right", width: "100px", height: "100px"});
    //   } else {
    //     doc.createImgFloatWithBase64(header.imgLogo, {type: "png", float: "right"});
    //   }
    // }
    if (header.hasTitle) {
      doc.createTitle(header.title, {textAlign: header.headerAlign});
    }
    if (header.hasSubTitle) {
      doc.createSubTitle(header.subTitle, {textAlign: header.headerAlign});
    }
  }
  if (data.hasExperiment) {
    doc.createH1("实验");
    let experiment = data.experiment;
    if (experiment.hasInformation) {
      doc.createH2("信息");
      let information = experiment.information;
      doc.createText("名称 : ", information.name);
      doc.createText("编号 : ", information.barcode);
      doc.createText("用户名 : ", information.userName);
      doc.createText("仪器名称 : ", information.instrumentName);
      doc.createText("模块类型 : ", information.blockType);
    }
    if (experiment.hasExperimentSetting) {
      doc.createH2("实验设置");
      let experimentSetting = experiment.experimentSetting;
      doc.createText("实验类型 : ", experimentSetting.experimentIdentify);
      doc.createText("试剂类型 : ", experimentSetting.reagentsType);
      doc.createText("运行模式 : ", experimentSetting.runMode);
    }
    if (experiment.hasParameterSetting) {
      doc.createH2("参数设置");
      let parameterSetting = experiment.parameterSetting;
      doc.createText("反应体积 : ", parameterSetting.reactionVolume + "μL");
      doc.createText("是否打开热盖 : ", parameterSetting.lidHeatingSwitch);
      doc.createText("热盖温度 : ", parameterSetting.coverTemp + "℃");
      doc.createText("热盖关闭温度 : ", parameterSetting.lidCloseTemp + "℃");
    }
    if (experiment.hasComments) {
      doc.createH2("备注");
      doc.createText(experiment.comments);
    }
    if (experiment.hasLoadPreview) {
      doc.createH2("加载预览");
      doc.createImgInLineWithBase64(experiment.imgLoadPreviewPlate);
      doc.createImgInLineWithBase64(experiment.imgLoadPreviewProtocol);
    }
  }
  if (data.hasPlate) {
    let plate = data.plate;
    doc.createH1("孔板设置");
    if (plate.hasPlateLayout) {
      doc.createH2("孔板设置");
      doc.createTable(plate.plateLayout, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}, leftHeadNum: 1, leftHeadStyle: {fontWeight: "bold"}});
    }
    if (plate.hasTask) {
      doc.createH2("类型");
      plate.task.split("\n").forEach(text => doc.createText(text));
    }
    if (plate.hasTarget) {
      doc.createH2("目标基因");
      doc.createTable(plate.target, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    // if (plate.hasSplitPlate) {
    //   doc.createH2("Split Plate");
    //   plate.splitPlate.split("\n").forEach(text => doc.createText(text));
    // }
    if (plate.hasSample) {
      doc.createH2("实验样品");
      doc.createTable(plate.sample, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (plate.hasStandard) {
      doc.createH2("标准曲线");
      let standard = plate.standard;
      doc.createText("选择目标基因 : ", standard.selectTarget);
      doc.createText("起始浓度 : ", standard.standardAmount);
      doc.createText("标准品数量 : ", standard.standardNumber);
      // doc.createText("Factor : ", standard.factor);
      doc.createText("倍比稀释 : ", standard.dilutionFactor);
      doc.createText("单位 : ", standard.unit);
      doc.createText("重复数 : ", standard.replicates);
      doc.createText("升序/降序： ", standard.increasing_decreasing);
      //  TODO:
      //    doc.createText("Assignment : ", "");
    }
  }
  if (data.hasProtocol) {
    doc.createH1("程序设置");
    let protocol = data.protocol;
    if (protocol.hasProtocol) {
      doc.createH2("程序");
      let arr = [];
      protocol.protocol.split("\n").forEach(text => {
        if (text.includes("\t")) {
          arr[arr.length] = text.split("\t");
        } else {
          if (arr.length !== 0) {
            doc.createTable(arr);
            arr = [];
          }
          doc.createText(text);
        }
      });
      if (arr.length !== 0) {
        doc.createTable(arr);
      }
    }
    doc.createText("程序运行图", "");
    doc.createImgInLineWithBase64(protocol.imgProtocolGraph, {style: {width: "100%", backgroundColor: "#213040"}});
    let arr = [];
    protocol.advancedSetting.split("\n").forEach(text => {
      if (text.includes("\t")) {
        arr[arr.length] = text.split("\t");
      } else {
        if (arr.length !== 0) {
          doc.createTable(arr);
          arr = [];
        }
        doc.createText(text);
      }
    });
    if (arr.length !== 0) {
      doc.createTable(arr);
    }
  }
  if (data.hasRun) {
    doc.createH1("实时运行");
    let run = data.run;
    if (run.hasRunTime) {
      doc.createH2("运行时间");
      run.runTime.split("\n").forEach(text => doc.createText(text));
    }
    if (run.hasTemperatureCurve) {
      doc.createH2("温度曲线");
      doc.createImgInLineWithBase64(run.imgTemperatureCurve, {style: {width: "100%"}});
    }
  }
  if (data.hasAnalysis) {
    doc.createH1("数据分析");
    let analysis = data.analysis;
    if (analysis.hasQuantification) {
      doc.createH2("定量分析");
      doc.createText("扩增曲线-Cq", "");
      doc.createImgInLineWithBase64(analysis.imgAmplificationCurve_cq, {style: {transform: "scale(0.6)", position: "relative", left: "-150px"}});
      doc.createText("扩增曲线-log", "");
      doc.createImgInLineWithBase64(analysis.imgAmplificationCurve_log, {style: {transform: "scale(0.6)", position: "relative", left: "-150px"}});
    }
    if (analysis.hasStandardCurve) {
      doc.createH2("标准曲线");
      doc.createImgInLineWithBase64(analysis.imgStandardCurve, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      analysis.standardCurveFormulas.forEach(formula => doc.createText(formula));
    }
    if (analysis.hasQuantificationData) {
      doc.createH2("定量数据");
      doc.createTable(analysis.quantificationData, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (analysis.hasMelt) {
      doc.createH2("熔解曲线");
      doc.createText("熔解曲线");
      doc.createImgInLineWithBase64(analysis.imgMeltCurve, {style: {transform: "scale(0.6)", position: "relative", left: "-150px"}});
      doc.createText("熔解曲线峰值");
      doc.createImgInLineWithBase64(analysis.imgMeltPeak, {style: {transform: "scale(0.7)", position: "relative", left: "-100px"}});
    }
    if (analysis.hasMeltData) {
      doc.createH2("熔解曲线结果");
      doc.createTable(analysis.meltData, {topHeadNum: 1, topHeadStyle: {fontWeight: "bold"}});
    }
    if (analysis.hasGeneExpression) {
      doc.createH2("基因表达");
      doc.createText("基因表达图谱", "");
      doc.createImgInLineWithBase64(analysis.imgGeneExpressionBar, {style: {width: "100%"}});
      doc.createText("分析模式 : ", "");
      analysis.geneExpressionMode.split("\n").forEach((item, index) => {
        doc.createText(item);
      });
      doc.createText("基因表达分析数据", "");
      doc.createTable(analysis.geneExpressionData);
    }
    if (analysis.hasAllelic) {
      doc.createH1("等位基因分析");
      doc.createText("等位基因分析-Cq", "");
      doc.createImgInLineWithBase64(analysis.imgAllelic_cq, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      doc.createText("等位基因分析-Rfu", "");
      doc.createImgInLineWithBase64(analysis.imgAllelic_rfu, {style: {transform: "scale(0.6)", position: "relative", left: "-100px"}});
      doc.createH1("等位基因分析数据");
      doc.createTable(analysis.allelicData);
    }
    if (analysis.hasQuanlityControl) {
      doc.createH2("质量控制");
      doc.createText("质量控制数据");
      doc.createTable(analysis.failureReportData);
    }
    if (analysis.hasEndPoint) {
      doc.createH2("终点法");
      doc.createText("终点法-选择与设置", "");
      doc.createTable(analysis.endPointSettingData);
      doc.createText("终点法荧光值结果", "");
      doc.createTable(analysis.endPointData);
    }
  }
  // console.log(data);
}

export function translateGene(item) {
  if (item == "Normalized Expression (ΔΔCq)") {
    return "归一化表达分析(ΔΔCq)";
  } else if (item == "RQ vs Sample") {
    return "RQ vs 样品";
  } else if (item == "Linear") {
    return "线性";
  } else if (item == "Vertical Bars") {
    return "垂直";
  } else {
    return item = item;
  }
}